package com.atguigu.gmall.order.listener;

import com.atguigu.gmall.common.rabbit.service.MsgRetryService;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SkuWareStockDeductListener {

    @Autowired
    private OrderInfoService orderInfoService ;

    @Autowired
    private MsgRetryService msgRetryService ;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue.ware.order" , durable = "true") ,
            exchange = @Exchange(value = "exchange.direct.ware.order" , durable = "true" , type = ExchangeTypes.DIRECT) ,
            key = { "ware.order" }
    ))
    public void skuWareStockDeductListener(Message message , Channel channel){

        // 获取消息的标签
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        // 获取消息
        byte[] body = message.getBody();
        String msg = new String(body) ;

        try {
            // 调用service更改订单的状态
            orderInfoService.skuWareStockDeductUpateOrderStatus(msg) ;

            // 进行应答
            channel.basicAck(deliveryTag , true);

        } catch (IOException e) {
            e.printStackTrace();
            msgRetryService.msgRetry(msg , deliveryTag , channel , 5);
        }

    }

}






















